package tree;

import java.util.ArrayList;
import java.util.Random;

/**
 * Class for generating randomized words of particular lengths.
 * 
 * @author Axel Sundbom, Johannes Sjölund, Tim Danielsson
 * 
 */
public class RandomWordFactory {

	Random random;
	ArrayList<String> words;
	int size;
	int maxWordSize;
	int minWordSize;

	public RandomWordFactory(int size, int minWordSize, int maxWordSize) {
		this.size = size;
		this.maxWordSize = maxWordSize;
		this.minWordSize = minWordSize;
		this.words = new ArrayList<String>();
		random = new Random();
	}

	/**
	 * Creates a list of random words.
	 */
	public void initializeRandoms() {
		String alpha = "abcdefghijklmnopqrstuvwxyz";
		int wordSize = minWordSize;

		for (int j = 0; j < size; j++) {

			if (maxWordSize != minWordSize) {
				wordSize = random.nextInt(maxWordSize - minWordSize)
						+ minWordSize;
			}

			char[] word = new char[wordSize];
			for (int i = 0; i < word.length; i++) {
				word[i] = alpha.charAt(random.nextInt(alpha.length()));
			}
			words.add(String.valueOf(word));
		}
	}

	/**
	 * 
	 * @return A random word.
	 */
	public String nextRandom() {
		if (words.size() > 0) {
			return words.remove(0);
		} else {
			return "";
		}
	}

	/**
	 * 
	 * @param i
	 *            Index
	 * @return A random word from a particular index.
	 */
	public String getRandom(int i) {
		if (words.size() > i) {
			return words.get(i);
		} else {
			return "";
		}
	}
}
